import json
import sc
#print(['playerInfo']['nickname'])

def user_(uid):
    file_path = 'userdata\\{}\\{}.json'.format(uid, uid)
    with open(file_path, 'r') as file:
        data = json.load(file)
        # print(data)

    user_name = data['playerInfo']['nickname']  # 用户名
    level = data['playerInfo']['level']  # 等级
    # signature = data['playerInfo']['signature'] #签名
    wordlevel = data['playerInfo']['worldLevel']  # 世界等级
    towerFloorIndex = data['playerInfo']['towerFloorIndex']  # 深渊层
    towerLevelIndex = data['playerInfo']['towerLevelIndex']  # 深渊间
    print('UID：', uid,
          '\n游戏账号:', user_name,
          '\n等   级:', level,
          '\n世界等级:', wordlevel,
          '\n深   渊:', towerFloorIndex, '-', towerLevelIndex,
          '\n')
    print('-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*')
    file_path = 'userdata\\{}\\{}.json'.format(uid, uid)
    with open(file_path, 'r') as file:
        data = json.load(file)

    with open('data\\n.json', 'r', encoding='utf-8') as file:
        name_id = json.load(file)
    role_arr = []
    role_arr_zh = []
    for i in data['avatarInfoList']:
        role_id = i['avatarId']
        role_arr.append(role_id)
        role_arr_zh.append(name_id[str(role_id)])
    return role_arr_zh,role_arr

def locality_data(uid):
    file_path = 'userdata\\{}\\{}.json'.format(uid,uid)
    with open(file_path, 'r') as file:
        data = json.load(file)
        #print(data)

    user_name = data['playerInfo']['nickname']   #用户名
    level = data['playerInfo']['level']   #等级
    #signature = data['playerInfo']['signature'] #签名
    wordlevel = data['playerInfo']['worldLevel']   #世界等级
    towerFloorIndex = data['playerInfo']['towerFloorIndex']   #深渊层
    towerLevelIndex = data['playerInfo']['towerLevelIndex']   #深渊间
    print('UID：',uid,
          '\n游戏账号:',user_name,
          '\n等   级:',level,
          '\n世界等级:',wordlevel,
          '\n深   渊:',towerFloorIndex,'-',towerLevelIndex,
          '\n')
    print('-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*')


    with open('data\\n.json', 'r', encoding='utf-8') as file:
        name_id = json.load(file)

    for i in data['avatarInfoList']:
        role_id = i['avatarId']
        role_fightPropMap = i['fightPropMap']
        print('角色天赋:')
        for j in i['skillLevelMap'].values():

            print(j,end=' ')
        print()

        #武器
        with open('data\\weapon.json', 'r', encoding='utf-8') as file:
            weapon_id = json.load(file)


        try:
            itemID = i['equipList'][5]['itemId']   #武器ID用于采集武器编码
            weapon = i['equipList'][5]['flat']['nameTextMapHash']   #武器hash值
            WeaponRefining = i['equipList'][5]['weapon']['affixMap']['1' +str(itemID)] + 1   #计算武器精炼
            weapon_level = i['equipList'][5]['weapon']['level']   #武器等级
            #以下相同，用于没有 [5]
        except:
            try:
                itemID = i['equipList'][4]['itemId']
                weapon = i['equipList'][4]['flat']['nameTextMapHash']
                WeaponRefining = i['equipList'][4]['weapon']['affixMap']['1' +str(itemID)] + 1
                weapon_level = i['equipList'][4]['weapon']['level']
            except:
                itemID = i['equipList'][1]['itemId']
                weapon = i['equipList'][1]['flat']['nameTextMapHash']
                WeaponRefining = i['equipList'][1]['weapon']['affixMap']['1' + str(itemID)] + 1
                weapon_level = i['equipList'][1]['weapon']['level']


        weapon = weapon_id[weapon]

        #生命值
        try:
            blood = role_fightPropMap['1'] + role_fightPropMap['1']*role_fightPropMap['3'] + role_fightPropMap['2']
        except:
            try:
                blood = role_fightPropMap['1'] + role_fightPropMap['2']
            except:
                try:
                    blood = role_fightPropMap['1'] + role_fightPropMap['1'] * role_fightPropMap['3']
                except:
                    blood = role_fightPropMap['1']

        #攻击
        try:
            attack = role_fightPropMap['4'] + role_fightPropMap['4']*role_fightPropMap['6'] + role_fightPropMap['5']
        except:
            try:
                attack = role_fightPropMap['4'] + role_fightPropMap['5']
            except:
                try:
                    attack = role_fightPropMap['4'] + role_fightPropMap['4'] * role_fightPropMap['6']
                except:
                    attack = role_fightPropMap['4']

        #防御
        try:
            defense = role_fightPropMap['7'] + role_fightPropMap['7'] * role_fightPropMap['9'] + role_fightPropMap['8']
        except:
            try:
                defense = role_fightPropMap['7'] + role_fightPropMap['7'] * role_fightPropMap['9']
            except:
                try:
                    defense = role_fightPropMap['7'] + role_fightPropMap['8']
                except:
                    defense = role_fightPropMap['7']

        #元素精通
        try:
            ElementalMystery = role_fightPropMap['28']
        except:
            ElementalMystery = 0

        #暴击率
        try:
            CriticalHitRate = role_fightPropMap['20'] * 100
        except:
            CriticalHitRate = 0.00

        #暴击伤害
        try:
            CriticalDamage = role_fightPropMap['22'] * 100
        except:
            CriticalDamage = 0.00

        try:
            ChargingEfficiency = role_fightPropMap['23'] * 100
        except:
            ChargingEfficiency = 0.00

        #命之座
        try:
            role_talentIdList = len(i['talentIdList'])
        except:
            role_talentIdList = 0

        print('角色名:', name_id[str(role_id)])
        print('武器:',weapon,end='  ')
        print('武器等级:',weapon_level,end='  ')
        print('武器精炼:',WeaponRefining)
        print('命之座:', role_talentIdList)
        print('生命值:%.0f'%(blood),end='        ')
        print('元素精通:%.0f'%(ElementalMystery))

        print('攻 击:%.0f'%(attack),end='          ')
        print('暴击率:%.1f'%(CriticalHitRate),end='%\n')

        print('防御力:%.0f'%(defense),end='          ')
        print('暴击伤害:%.1f'%(CriticalDamage),end='%\n')

        print('充能效率:%.2f'%(ChargingEfficiency),end='%          ')

        print('\n-------------------------------------------------------------')
    print('当前UID:', uid)

def DirectedQuery(r,uid):
    file_path = 'userdata\\{}\\{}.json'.format(uid, uid)
    with open(file_path, 'r') as file:
        data = json.load(file)

    with open('data\\n.json', 'r', encoding='utf-8') as file:
        name_id = json.load(file)

    flag = 1
    all_score = 0
    for i in data['avatarInfoList']:
        if flag == r:
            role_id = i['avatarId']
            print(name_id[str(role_id)])
            role_fightPropMap = i['fightPropMap']
            print('角色天赋:')
            for j in i['skillLevelMap'].values():
                print(j, end=' ')
            print()

            # 武器
            with open('data\\weapon.json', 'r', encoding='utf-8') as file:
                weapon_id = json.load(file)

            try:
                itemID = i['equipList'][5]['itemId']  # 武器ID用于采集武器编码
                weapon = i['equipList'][5]['flat']['nameTextMapHash']  # 武器hash值
                WeaponRefining = i['equipList'][5]['weapon']['affixMap']['1' + str(itemID)] + 1  # 计算武器精炼
                weapon_level = i['equipList'][5]['weapon']['level']  # 武器等级
                # 以下相同，用于没有 [5]
            except:
                try:
                    itemID = i['equipList'][4]['itemId']
                    weapon = i['equipList'][4]['flat']['nameTextMapHash']
                    WeaponRefining = i['equipList'][4]['weapon']['affixMap']['1' + str(itemID)] + 1
                    weapon_level = i['equipList'][4]['weapon']['level']
                except:
                    itemID = i['equipList'][0]['itemId']
                    weapon = i['equipList'][0]['flat']['nameTextMapHash']
                    WeaponRefining = i['equipList'][0]['weapon']['affixMap']['1' + str(itemID)] + 1
                    weapon_level = i['equipList'][0]['weapon']['level']

            weapon = weapon_id[weapon]

            # 生命值
            try:
                blood = role_fightPropMap['1'] + role_fightPropMap['1'] * role_fightPropMap['3'] + role_fightPropMap[
                    '2']
            except:
                try:
                    blood = role_fightPropMap['1'] + role_fightPropMap['2']
                except:
                    try:
                        blood = role_fightPropMap['1'] + role_fightPropMap['1'] * role_fightPropMap['3']
                    except:
                        blood = role_fightPropMap['1']

            # 攻击
            try:
                attack = role_fightPropMap['4'] + role_fightPropMap['4'] * role_fightPropMap['6'] + role_fightPropMap[
                    '5']
            except:
                try:
                    attack = role_fightPropMap['4'] + role_fightPropMap['5']
                except:
                    try:
                        attack = role_fightPropMap['4'] + role_fightPropMap['4'] * role_fightPropMap['6']
                    except:
                        attack = role_fightPropMap['4']

            # 防御
            try:
                defense = role_fightPropMap['7'] + role_fightPropMap['7'] * role_fightPropMap['9'] + role_fightPropMap[
                    '8']
            except:
                try:
                    defense = role_fightPropMap['7'] + role_fightPropMap['7'] * role_fightPropMap['9']
                except:
                    try:
                        defense = role_fightPropMap['7'] + role_fightPropMap['8']
                    except:
                        defense = role_fightPropMap['7']

            # 元素精通
            try:
                ElementalMystery = role_fightPropMap['28']
            except:
                ElementalMystery = 0

            # 暴击率
            try:
                CriticalHitRate = role_fightPropMap['20'] * 100
            except:
                CriticalHitRate = 0.00

            # 暴击伤害
            try:
                CriticalDamage = role_fightPropMap['22'] * 100
            except:
                CriticalDamage = 0.00

            try:
                ChargingEfficiency = role_fightPropMap['23'] * 100
            except:
                ChargingEfficiency = 0.00

            # 命之座
            try:
                role_talentIdList = len(i['talentIdList'])
            except:
                role_talentIdList = 0

            print('角色名:', name_id[str(role_id)])
            print('武器:', weapon, end='  ')
            print('武器等级:', weapon_level, end='  ')
            print('武器精炼:', WeaponRefining)
            print('命之座:', role_talentIdList)
            print('生命值:%.0f' % (blood), end='        ')
            print('元素精通:%.0f' % (ElementalMystery))

            print('攻 击:%.0f' % (attack), end='          ')
            print('暴击率:%.1f' % (CriticalHitRate), end='%\n')

            print('防御力:%.0f' % (defense), end='          ')
            print('暴击伤害:%.1f' % (CriticalDamage), end='%\n')

            print('充能效率:%.2f' % (ChargingEfficiency), end='%          ')

            # 圣遗物
            print('\n\n--------------圣遗物--------------\n')
            with open('data\\loc.json', 'r', encoding='utf-8') as file:
                syw_name = json.load(file)
            jikun = 0
            for k in range(0, 5):
                try:
                    syw_type = i['equipList'][k]['flat']['equipType']
                    syw_id = i['equipList'][k]['flat']['setNameTextMapHash']

                    syw_name_ = syw_name[syw_id]
                    syw_type_ = syw_name[syw_type]

                    syw_lv = int(i['equipList'][k]['reliquary']['level']) - 1
                    syw_lv = str(syw_lv) + '级'
                    # print(syw_name_, end='|')
                    # print(syw_type_,end=':\n')
                    print("{:<8}{:<5}{}".format(syw_name_, syw_lv, syw_type_), end=':\n主词条:')

                    # 主词条
                    jikun += 1
                    MainParameter = i['equipList'][k]['flat']['reliquaryMainstat']['mainPropId']  # 属性名
                    MainParameter_num = i['equipList'][k]['flat']['reliquaryMainstat']['statValue']  # 属性值



                    if isinstance(MainParameter_num, float):
                        output = "%.2f%%" % MainParameter_num
                    else:
                        output = str(MainParameter_num)
                    print(syw_name[MainParameter], output)
                    score_ = sc.ty_role(role_id, syw_name[MainParameter])
                    # 小词条
                    try:
                        #score_ = 0
                        score = 0
                        for substat in i['equipList'][k]['flat']['reliquarySubstats']:

                            appendPropId = substat['appendPropId']
                            appendPropId_num = substat['statValue']

                            if isinstance(appendPropId_num, float):
                                output_ = "%.2f%%" % appendPropId_num
                            else:
                                if syw_name[appendPropId] == '暴击伤害':
                                    output_ = str(appendPropId_num) +'.0%'
                                if syw_name[appendPropId] == '元素充能效率':
                                    output_ = str(appendPropId_num) +'.0%'
                                if syw_name[appendPropId] == '暴击率':
                                    output_ = str(appendPropId_num) +'.0%'
                                else:
                                    output_ = str(appendPropId_num)

                            try:
                                score = sc.sc(role_id, syw_name[appendPropId], float(appendPropId_num))
                            except Exception as err:
                                score = score
                                print(err)
                            score_ += score
                            print(syw_name[appendPropId], output_, end=' ')
                        all_score += score_
                        print('%.1f'%(score_),end='分\n\n')

                    except Exception as err:
                        print(err)
                        pass
                except Exception as err:
                    print(err)
                    pass
            break
        else:
            flag += 1
    print('当前角色总分:%.2f'%(all_score))
    print('-*-*-*-*-*-*-*-*-*-*-*-*-*-')
def roles(uid,num):
    r = int(num)
    DirectedQuery(r,uid)

#locality_data(173057690)
#roles(173057690,1)
